﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.Common;

namespace WarehouseManager.View
{
    public partial class DrugSelectForm : Form
    {
        private string stroperator;
        private DataTable datSource;
        private bool bSetInfpFlg = false;


        public SetInfoCallBack SelectDurgInfo { get; set; }

        public DrugSelectForm(string _operator, DataTable DatInfo)
        {
            InitializeComponent();

            this.stroperator = _operator;
            this.Init(DatInfo);
        }

        private void Init(DataTable DatInfo)
        {
            this.lblOperator.Text = this.stroperator;
            this.lblOpDate.Text = DateTimeUtil.GetCurrentDate();

            this.datSource = this.CreateDataSource();
            this.datGridList.DataSource = this.datSource;

            this.SetDataSource(DatInfo);

        }

        private DataTable CreateDataSource()
        {
            DataTable datSource = new DataTable();
            datSource.Columns.Add(ConstInfo.COL_DRUGID, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_DRUGNAME, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_ARTNO, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_UNIT, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_DRUGSPEC, typeof(string));
            datSource.Columns.Add(ConstInfo.COL_PRODUCEAREA, typeof(string));
            datSource.AcceptChanges();
            return datSource;
        }

        private void SetDataSource(DataTable datDurgInfo)
        {
            if (datDurgInfo == null || datDurgInfo.Rows.Count == 0)
            {
                return;
            }

            foreach (DataRow row in datDurgInfo.Rows)
            {
                DataRow rowTemp = this.datSource.NewRow();
                rowTemp[ConstInfo.COL_DRUGID] = row[ConstInfo.COL_DRUGID];
                rowTemp[ConstInfo.COL_DRUGNAME] = row[ConstInfo.COL_DRUGNAME];
                rowTemp[ConstInfo.COL_ARTNO] = row[ConstInfo.COL_ARTNO];
                rowTemp[ConstInfo.COL_UNIT] = row[ConstInfo.COL_UNIT];
                rowTemp[ConstInfo.COL_DRUGSPEC] = row[ConstInfo.COL_DRUGSPEC];
                rowTemp[ConstInfo.COL_PRODUCEAREA] = row[ConstInfo.COL_PRODUCEAREA];
                this.datSource.Rows.Add(rowTemp);
            }
            this.datSource.AcceptChanges();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.datGridList.SelectedRows.Count != 1)
            {
                this.errorProvider.SetError(this.btnSelect, "请选中一行再操作");
                return;
            }

            DataRow rowSelect = this.datSource.Rows[this.datGridList.SelectedRows[0].Index];

            if (SelectDurgInfo != null)
            {
                SelectDurgInfo(rowSelect);
            }
            bSetInfpFlg = true;
            this.Close();
        }       

        private void datGridList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0 || e.RowIndex < 0)
            {
                return;
            }

            DataRow rowSelect = this.datSource.Rows[this.datGridList.SelectedRows[0].Index];

            if (this.SelectDurgInfo != null)
            {
                SelectDurgInfo(rowSelect);
            }
            bSetInfpFlg = true;
            this.Close();
        }

        private void DrugSelectForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bSetInfpFlg)
            {
                return;
            }

            DataRow rowSelect = null;
            if (this.datGridList.SelectedRows.Count != 1)
            {
                rowSelect = this.datSource.Rows[0];
            }
            else
            {
                rowSelect = this.datSource.Rows[this.datGridList.SelectedRows[0].Index];
            }

            if (this.SelectDurgInfo != null)
            {
                SelectDurgInfo(rowSelect);
            }
        }
    }
}
